package com.pur.zsms.matermanage.mater.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.service.MaterComputeService;
import com.pur.zsms.quotation.dao.IQuotationDao;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.shopmanage.po.ShopRaisePricePo;

@Service("materComputerService")
public class MaterComputerServiceImpl implements MaterComputeService {
	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;
	@Resource(name = "quotationDao")
	private IQuotationDao quotationDao;
	public IShopDao getShopDaoImpl() {
		return shopDaoImpl;
	}

	public void setShopDaoImpl(IShopDao shopDaoImpl) {
		this.shopDaoImpl = shopDaoImpl;
	}

	// 抬价计算模块
	public BigDecimal ComputeMaterRaise(BigDecimal materValueUndressedCost, int materId, int supId, long shopId) throws GenericException {
		
		long companyId =  shopDaoImpl.getShopPoByShopId(shopId).get(0).getCompanyUser();
		List<Object[]> materValue = quotationDao.getCompanyPeriodValue(materId, supId,companyId);
		if (materValue != null && materValue.size() > 0) {
			Object value = materValue.get(0);
			return new BigDecimal(value.toString());
		} 
		
		List<ShopRaisePricePo> shopRaisePricePoList = shopDaoImpl.getShopRaisePricePoByShopIdMaterIdTypeSupId(shopId, materId, supId);
		if (shopRaisePricePoList == null || shopRaisePricePoList.size() == 0) {
			// 如果没有关系则返回进货价钱
			return materValueUndressedCost;
		}
		BigDecimal materValueUndressed = materValueUndressedCost
				.multiply(shopRaisePricePoList.get(0).getRaiseAmount().multiply(new BigDecimal(0.01)));
		return materValueUndressed.setScale(1, BigDecimal.ROUND_UP);
	}

	public BigDecimal getMaterValueRealLastByMaterId(int materId, int supId, long shopId) throws GenericException {
		return materDaoImpl.getMaterValueRealLastByMaterId(materId, supId, shopId);
	}
}
